%load alt_calib/eq_mit_mass_shock.mat
clear
%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
load ../../prod/calibration_v3/kimball/eq_mit_mass.mat
%load ../prod/calibration_v3/sandbox/eq_mit_mass

set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')

%%
Ns = 1 + [0 -0.06048771 -3.57246795 -5.52645038 -6.69652130 -6.36985428 -6.33560655 -6.29328993]/100;

adj = sum(eq_mit.mu,1);
addpath ../
scx = pick_scale(adj, Ns);

T = 15;

%%
figure(47)
 
 Labor       =  eq_mit.l;
 total_labor =  sum(eq_mit.mu.*Labor);

 % 
 costs = eq_mit.l;
 prods = repmat(exp(glob.sf(:,2)), 1, options.T);

 wages = repmat(eq_mit.W', glob.Nsf, 1);
 mkps  = eq_mit.p./(wages./prods);
 mu = mkps;
% 
 tc    = sum(wages.*costs.*eq_mit.mu);
 num    = sum(wages.*costs.*eq_mit.mu.*mkps);
% 
 mu_cw = num./tc;

Ys    = repmat(eq_mit.C',glob.Nsf,1);
Z     = sum(eq_mit.y./Ys./prods.*eq_mit.mu);
Z     = Z.^(-1);


%% plot real output, labor demand, and nominal gdp
close all

Y = log_linear_scale(eq_mit.C, scx);

W = log_linear_scale(eq_mit.W, scx);

L = plot(log_linear_scale(total_labor, scx)/total_labor(end), 'LineWidth', 4);


ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);

labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = labor_bill./ngdp;

mu = log_linear_scale(1./labor_share, scx)

tfp = Z;%eq_mit.C'./total_labor;
A = log_linear_scale(tfp, scx)/tfp(end);

%% Counterfactuals
dates = 2007:(2007+300-1);

bl = three_equation_model(A, mu, .5);
fixed_mu = three_equation_model(A, mu(end), .5);
fixed_tfp = three_equation_model(A(end), mu, .5);

subplot(2,2,1)
plot(dates, bl.Y, 'LineWidth', 4)
hold on
plot(dates, fixed_mu.Y, 'r--', 'LineWidth', 4)
plot(dates, fixed_tfp.Y, '-.', 'LineWidth', 4)
legend('Baseline', 'TFP effect', 'Markup effect', 'Location', 'SouthEast')
xlim([2007 2020]);
title('Y')

subplot(2,2,2)
plot(dates, bl.L, 'LineWidth', 4)
hold on
plot(dates, fixed_mu.L, 'r--', 'LineWidth', 4)
plot(dates, fixed_tfp.L, '-.', 'LineWidth', 4)
xlim([2007 2020]);
title('L')

subplot(2,2,3)
plot(dates, bl.W, 'LineWidth', 4)
hold on
plot(dates, fixed_mu.W, 'r--', 'LineWidth', 4)
plot(dates, fixed_tfp.W, '-.', 'LineWidth', 4)
xlim([2007 2020]);
title('W')

subplot(2,2,4)
plot(dates, A/A(end), 'LineWidth', 4)
hold on
plot(dates, mu/mu(end),'r--','LineWidth', 4)
xlim([2007 2020]);
legend('TFP','Markup')

set(gcf,'units','points','position',[10,10,1000,600])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'entry_shock_decomp_gr.png')